package com.GlobalPaint.app.ui;

import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.ui.Home.HomeFragment;
import com.GlobalPaint.app.ui.Laborer.LaborerFragment;
import com.GlobalPaint.app.ui.MessageFragment.MessageFragment;
import com.GlobalPaint.app.ui.Mine.MineFragment;
import com.GlobalPaint.app.ui.location.LocationFragment;
import com.GlobalPaint.app.utils.TabEntity;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
    private CommonTabLayout mTabLayout;
    private long mBackPressedTime;
    private String[] mTitles;
    private int[] mIconUnselectIds;
    private int[] mIconSelectIds;
    private ArrayList<CustomTabEntity> mTabEntities;
    private ArrayList<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      /*  CommUtility.verifyStoragePermissions(this);//添加高版本的权限
        ImmersionBar.with(this).init(); //初始化，默认透明状态栏和黑色导航栏*/
        initView();
     /*   ImmersionBar.with(this)
                .statusBarColor(R.color.bar)
                .fitsSystemWindows(true)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init();*/
    }

    private void initView() {
        mTabLayout=(CommonTabLayout) findViewById(R.id.main_tabLayout);
        mTitles = new String[] {
                "首页","用工","位置","消息","我的"
        };

        mIconUnselectIds = new int[]{
                R.drawable.icon_home, R.drawable.icon_laborer,R.drawable.icon_wz,
                R.drawable.icon_message,R.drawable.icon_mine,
        };

        mIconSelectIds = new int[]{
                R.drawable.icon_home1, R.drawable.icon_laborer1,R.drawable.icon_wz1,
                R.drawable.icon_message1, R.drawable.icon_mine1};
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new LaborerFragment());
        fragments.add(new LocationFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MineFragment());

        mTabEntities = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        mTabLayout.setTabData(mTabEntities,this,R.id.main_viewPager,fragments);
    }
    @Override
    public void onBackPressed() {

        boolean isDoubleClick = true;// BaseApplication.get(AppConfig.KEY_DOUBLE_CLICK_EXIT, true);

        if (isDoubleClick) {
            long curTime = SystemClock.uptimeMillis();
            if ((curTime - mBackPressedTime) < (3 * 1000)) {
                finish();
            } else {
                mBackPressedTime = curTime;
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_LONG).show();
            }
        } else {
            finish();
        }
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, ev)) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            return !(event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom);
        }
        return false;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy(); //不调用该方法，如果界面bar发生改变，在不关闭app的情况下，退出此界面再进入将记忆最后一次bar改变的状态
    }
}
